/*
 * v1.1 by jetsum tonydon
 * netbeansIDE 7.1
 */
package cn.txdnet.txweb.basedao.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import txdnet.util.Logger;

/**
 * mybatis配置工具类, 20120921后改用SqlSessionMgr管理
 * @author txdnet
 */
public class MybatisUtil {
    
    public static SqlSessionFactory factory;
    
            
    public static SqlSessionFactory getFactory(String env_id){
        SqlSessionFactory sqlSessionfactory = null;
        SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
        try{
            if(env_id==null){
                sqlSessionfactory = builder.build(Resources.getResourceAsReader("mybatis.cfg.xml"));
            }else{
                sqlSessionfactory = builder.build(Resources.getResourceAsReader("mybatis.cfg.xml"), env_id);
            }
        }catch(Exception e){
             Logger.error(MybatisUtil.class, e.toString().concat(".-getFactory()"));
        }
        return sqlSessionfactory;
    }
    
    public static SqlSession getSqlSession(boolean is_auto_commit){
        return factory.openSession(is_auto_commit);
    }
    
    /*
     * @note 该方法创建一个自动提交的会话，一般用于 select ,单个SQL的更新
     */
    public static SqlSession getSqlSession(){
        return MybatisUtil.getSqlSession(true);
    }
    
    static{
        factory = MybatisUtil.getFactory("jndi-bak");
    }
}
